package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.ClueActivityRelation;
import com.bjpowernode.crm.workbench.mapper.ClueActivityRelationMapper;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.service.ClueActivityRelationService;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class ClueActivityRelationServiceImpl implements ClueActivityRelationService {

    /**
     * 访问线索与市场活动的关联数据
     */
    private ClueActivityRelationMapper clueActivityRelationMapper;

    /**
     * 处理市场活动相关业务
     */
    private ActivityService activityService;

    /**
     * 处理用户相关业务
     */
    private UserService userService;

    @Override
    public List<Activity> queryActivitiesNotRelated(String clueId, String activityName) {
        //首先查询与该线索有关联的市场活动的 id
        List<String> idsOfRelatedActivities = this.clueActivityRelationMapper.selectIdsOfRelatedActivities(clueId);

        //接着查询除此以外的市场活动
        List<Activity> activities = this.activityService.queryActivitiesByIdsOfExclusions(idsOfRelatedActivities, activityName);

        //市场活动中存放的是所有者、创建者、最后一个编辑者的 id ，页面需要的是他们的名称
        for (Activity activity : activities) {
            activity.setOwner(this.userService.queryNameById(activity.getOwner()));
            activity.setCreateBy(this.userService.queryNameById(activity.getCreateBy()));
            activity.setEditBy(this.userService.queryNameById(activity.getEditBy()));
        }

        return activities;
    }

    @Override
    public void relateActivitiesByIds(String[] activityIds, String clueId) {
        //封装每一条关联信息，并将他们添加到集合中，交给数据访问层
        List<ClueActivityRelation> clueActivityRelations = new ArrayList<>(activityIds.length);
        ClueActivityRelation temp = null;

        for (String everyId : activityIds) {
            temp = new ClueActivityRelation();
            temp.setId(UUIDUtils.getUUID());
            temp.setClueId(clueId);
            temp.setActivityId(everyId);

            clueActivityRelations.add(temp);
        }

        this.clueActivityRelationMapper.insertClueActivityRelations(clueActivityRelations);
    }

    @Override
    public void deleteClueActivityRelationByClueIdAndActivityId(String clueId, String activityId) {
        this.clueActivityRelationMapper.deleteClueActivityRelationByClueIdAndActivityId(clueId, activityId);
    }

    @Override
    public List<Activity> queryRelatedActivities(String activityName, String clueId) {
        //首先查询与该线索有关联的市场活动的 id
        List<String> activityIds = this.clueActivityRelationMapper.selectIdsOfRelatedActivities(clueId);

        //然后再根据 id 查询这些市场活动
        List<Activity> activities = this.activityService.queryActivitiesByIdsAndName(activityName, activityIds);

        //市场活动中存放的是所有者、创建者、最后一个编辑者的 id ，页面需要的是他们的名称
        for (Activity activity : activities) {
            activity.setOwner(this.userService.queryNameById(activity.getOwner()));
            activity.setCreateBy(this.userService.queryNameById(activity.getCreateBy()));
            activity.setEditBy(this.userService.queryNameById(activity.getEditBy()));
        }

        return activities;
    }

    @Override
    public List<ClueActivityRelation> queryClueActivityRelationsByClueId(String clueId) {
        return this.clueActivityRelationMapper.selectClueActivityRelationsByClueId(clueId);
    }

    @Override
    public List<String> queryIdsOfRelatedActivities(String clueId) {
        return this.clueActivityRelationMapper.selectIdsOfRelatedActivities(clueId);
    }

    @Override
    public void removeClueActivityRelationsByClueId(String clueId) {
        this.clueActivityRelationMapper.deleteClueActivityRelationsByClueId(clueId);
    }

}
